<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/components/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag, ElSwitch } from 'element-plus'
import { Table } from '@/components/Table'
import { getReviewTheProductList} from '@/api/commodity'
import { useTable } from './useTable'
import { TableData } from '@/api/table/types'
import { h, ref, unref, reactive } from 'vue'
import Write from './components/Write.vue'
import Detail from './components/Detail.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import { CommodityData } from '@/api/commodity/types'
import {FormSchema} from "@/types/form"

const { register, tableObject, methods, elTableRef } = useTable<TableData>({
  getListApi: getReviewTheProductList,
  // delListApi: delTableListApi,*/
  response: {
    list: 'list',
    total: 'total'
  },
  defaultParams: {
    title: 's'
  }
})

let asd;

const getAsd = async(params?: Params) => {
  const res = await getReviewTheProductList(
          params || {
            pageIndex: 1,
            pageSize: 11
          }
  )
          .catch(() => {})
          .finally(() => {})
  console.log(res)
  if(res){
    asd = res.data
  }
}

getAsd()

const { getList, setSearchParams } = methods

getList()

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'product_id',
    label: '序号',
    type: 'index',
  },
  {
    field: 'product_code',
    label: '商品编号'
  },
  {
    field: 'product_name',
    label: '商品名称'
  },
  {
    field: 'currentprice_product',
    label: '价格'
  },
  {
    field:'categroy_name',
    label:'商品分类'
  },
  {
    field:'true_name',
    label:'上传人员'
  },
  {
    field: 'product_sate',
    label: '审核状态',
    sortable: true,
    formatter: (row,column,cellValue,index) => {
      if (cellValue == 2){
        return "审核中"
      }else if(cellValue == 3){
        return "审核通过"
      }else {
        return "审核不通过"
      }
    }
  },
  //操作
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action')
  },

])

const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const AddAction = () => {
  dialogTitle.value = t('exampleDemo.add')
  tableObject.currentRow = null
  dialogVisible.value = true
  actionType.value = ''
}

const delLoading = ref(false)

const delData = async (row: TableData | null, multiple: boolean) => {
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.id) : [tableObject.currentRow?.id as string],
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}

const actionType = ref('')

const action = (row: TableData, type: string) => {
  dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  actionType.value = type
  tableObject.currentRow = row
  dialogVisible.value = true
}

const writeRef = ref<ComponentRef<typeof Write>>()

const loading = ref(false)

const save = async () => {
  const write = unref(writeRef)
  await write?.elFormRef?.validate(async (isValid) => {
    if (isValid) {
      loading.value = true
      const data = (await write?.getFormData()) as TableData
      const res = await saveTableApi(data)
        .catch(() => {})
        .finally(() => {
          loading.value = false
        })
      if (res) {
        dialogVisible.value = false
        tableObject.currentPage = 1
        getList()
      }
    }
  })
}

//定义搜索框
allSchemas.searchSchema = reactive<FormSchema[]>([
  {
    field: 'productName',
    label: '商品名称',
    component: 'Input'
  },
  {
    field: 'categroyName',
    label: '商品分类',
    component: 'Input'
  },
  {
    field: 'trueName',
    label: '上传人员',
    component: 'Input'
  },
])
</script>

<template>
  <ContentWrap :title="`筛选查询`">
    <Search
      :model="{ title: 's' }"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />
  </ContentWrap>

  <ContentWrap :title="`商品审核列表`">
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton
          type="success"
          v-hasPermi="['example:dialog:view']"
          @click="action(row, 'detail')"
        >
          {{ '查看' }}
        </ElButton>
        <ElButton type="primary" v-hasPermi="['example:dialog:edit']" @click="action(row, 'edit')">
          {{ '审核' }}
        </ElButton>
      </template>
      <template #state="{row }">
        <ElSwitch
                v-model="row.state"
                class="ml-2"
                inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                active-text="上架"
                inactive-text="下架"
                :active-value="1"
                :inactive-value="2"
        />
      </template>

    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Write
      v-if="actionType !== 'detail'"
      ref="writeRef"
      :form-schema="allSchemas.formSchema"
      :current-row="tableObject.currentRow"
    />

    <Detail
      v-if="actionType === 'detail'"
      :detail-schema="allSchemas.detailSchema"
      :current-row="tableObject.currentRow"
    />

    <template #footer>
      <ElButton v-if="actionType !== 'detail'" type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
    </template>
  </Dialog>
</template>
